home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / client / scripts / chatHud.cs < prev    next >
Encoding:
Text File  |  2005-11-23  |  8.7 KB  |  287 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. //-----------------------------------------------------------------------------
  7. // Message Hud
  8. //-----------------------------------------------------------------------------
  9.  
  10. // chat hud sizes
  11. $outerChatLenY[1] = 72;
  12. $outerChatLenY[2] = 140;
  13. $outerChatLenY[3] = 200;
  14.  
  15. // Only play sound files that are <= 5000ms in length.
  16. $MaxMessageWavLength = 5000;
  17.  
  18. // Helper function to play a sound file if the message indicates.
  19. // Returns starting position of wave file indicator.
  20. function playMessageSound(%message, %voice, %pitch)
  21. {
  22.    // Search for wav tag marker.
  23.    %wavStart = strstr(%message, "~w");
  24.    if (%wavStart == -1) {
  25.       return -1;
  26.    }
  27.  
  28.    %wav = getSubStr(%message, %wavStart + 2, 1000);
  29.    if (%voice !$= "") {
  30.       %wavFile = "~/data/sound/voice/" @ %voice @ "/" @ %wav;
  31.    }
  32.    else {
  33.       %wavFile = "~/data/sound/" @ %wav;
  34.    }
  35.    if (strstr(%wavFile, ".wav") != (strlen(%wavFile) - 4)) {
  36.       %wavFile = %wavFile @ ".wav";
  37.    }
  38.    // XXX This only expands to a single filepath, of course; it
  39.    // would be nice to support checking in each mod path if we
  40.    // have multiple mods active.
  41.    %wavFile = ExpandFilename(%wavFile);
  42.  
  43.    if ((%pitch < 0.5) || (%pitch > 2.0)) {
  44.       %pitch = 1.0;
  45.    }
  46.  
  47.    %wavLengthMS = alxGetWaveLen(%wavFile) * %pitch;
  48.    if (%wavLengthMS == 0) {
  49.       error("** WAV file \"" @ %wavFile @ "\" is nonexistent or sound is zero-length **");
  50.    }
  51.    else if (%wavLengthMS <= $MaxMessageWavLength) {
  52.       if ($ClientChatHandle[%sender] != 0) {
  53.          alxStop($ClientChatHandle[%sender]);
  54.       }
  55.       $ClientChatHandle[%sender] = alxCreateSource(AudioMessage, %wavFile);
  56.       if (%pitch != 1.0) {
  57.          alxSourcef($ClientChatHandle[%sender], "AL_PITCH", %pitch);
  58.       }
  59.       alxPlay($ClientChatHandle[%sender]);
  60.    }
  61.    else {
  62.       error("** WAV file \"" @ %wavFile @ "\" is too long **");
  63.    }
  64.  
  65.    return %wavStart;
  66. }
  67.  
  68.  
  69. // All messages are stored in this HudMessageVector, the actual
  70. // MainChatHud only displays the contents of this vector.
  71.  
  72. new MessageVector(HudMessageVector);
  73. $LastHudTarget = 0;
  74.  
  75.  
  76. //-----------------------------------------------------------------------------
  77. function onChatMessage(%message, %voice, %pitch)
  78. {
  79.    // XXX Client objects on the server must have voiceTag and voicePitch
  80.    // fields for values to be passed in for %voice and %pitch... in
  81.    // this example mod, they don't have those fields.
  82.  
  83.    // Clients are not allowed to trigger general game sounds with their
  84.    // chat messages... a voice directory MUST be specified.
  85.    if (%voice $= "") {
  86.       %voice = "default";
  87.    }
  88.  
  89.    // See if there's a sound at the end of the message, and play it.
  90.    if ((%wavStart = playMessageSound(%message, %voice, %pitch)) != -1) {
  91.       // Remove the sound marker from the end of the message.
  92.       %message = getSubStr(%message, 0, %wavStart);
  93.    }
  94.  
  95.    // Chat goes to the chat HUD.
  96.    if (getWordCount(%message)) {
  97.       ChatHud.addLine(%message);
  98.    }
  99. }
  100.  
  101. function onServerMessage(%message)
  102. {
  103.    // See if there's a sound at the end of the message, and play it.
  104.    if ((%wavStart = playMessageSound(%message)) != -1) {
  105.       // Remove the sound marker from the end of the message.
  106.       %message = getSubStr(%message, 0, %wavStart);
  107.    }
  108.  
  109.    // Server messages go to the chat HUD too.
  110.    if (getWordCount(%message)) {
  111.       ChatHud.addLine(%message);
  112.    }
  113. }
  114.  
  115.  
  116.  
  117. //-----------------------------------------------------------------------------
  118. // MainChatHud methods
  119. //-----------------------------------------------------------------------------
  120.  
  121. function MainChatHud::onWake( %this )
  122. {
  123.    // set the chat hud to the users pref
  124.    %this.setChatHudLength( $Pref::ChatHudLength );
  125. }
  126.  
  127.  
  128. //------------------------------------------------------------------------------
  129.  
  130. function MainChatHud::setChatHudLength( %this, %length )
  131. {
  132.    OuterChatHud.resize(firstWord(OuterChatHud.position), getWord(OuterChatHud.position, 1),
  133.                        firstWord(OuterChatHud.extent), $outerChatLenY[%length]);
  134.    ChatScrollHud.scrollToBottom();
  135.    ChatPageDown.setVisible(false);
  136. }
  137.  
  138.  
  139. //------------------------------------------------------------------------------
  140.  
  141. function MainChatHud::nextChatHudLen( %this )
  142. {
  143.    %len = $pref::ChatHudLength++;
  144.    if ($pref::ChatHudLength == 4)
  145.       $pref::ChatHudLength = 1;
  146.    %this.setChatHudLength($pref::ChatHudLength);
  147. }
  148.  
  149.  
  150. //-----------------------------------------------------------------------------
  151. // ChatHud methods
  152. // This is the actual message vector/text control which is part of
  153. // the MainChatHud dialog
  154. //-----------------------------------------------------------------------------
  155.  
  156. //-----------------------------------------------------------------------------
  157.  
  158. function ChatHud::addLine(%this,%text)
  159. {
  160.    //first, see if we're "scrolled up"...
  161.    %textHeight = %this.profile.fontSize;
  162.    if (%textHeight <= 0)
  163.       %textHeight = 12;
  164.    %chatScrollHeight = getWord(%this.getGroup().getGroup().extent, 1);
  165.    %chatPosition = getWord(%this.extent, 1) - %chatScrollHeight + getWord(%this.position, 1);
  166.    %linesToScroll = mFloor((%chatPosition / %textHeight) + 0.5);
  167.    if (%linesToScroll > 0)
  168.       %origPosition = %this.position;
  169.       
  170.    //add the message...
  171.    while( !chatPageDown.isVisible() && HudMessageVector.getNumLines() && (HudMessageVector.getNumLines() >= $pref::HudMessageLogSize))
  172.    {
  173.       %tag = HudMessageVector.getLineTag(0);
  174.       if(%tag != 0)
  175.          %tag.delete();
  176.       HudMessageVector.popFrontLine();
  177.    }
  178.    HudMessageVector.pushBackLine(%text, $LastHudTarget);
  179.    $LastHudTarget = 0;
  180.  
  181.    //now that we've added the message, see if we need to reset the position
  182.    if (%linesToScroll > 0)
  183.    {
  184.       chatPageDown.setVisible(true);
  185.       %this.position = %origPosition;
  186.    }
  187.    else
  188.       chatPageDown.setVisible(false);
  189. }
  190.  
  191.  
  192. //-----------------------------------------------------------------------------
  193.  
  194. function ChatHud::pageUp(%this)
  195. {
  196.    // Find out the text line height
  197.    %textHeight = %this.profile.fontSize;
  198.    if (%textHeight <= 0)
  199.       %textHeight = 12;
  200.  
  201.    // Find out how many lines per page are visible
  202.    %chatScrollHeight = getWord(%this.getGroup().getGroup().extent, 1);
  203.    if (%chatScrollHeight <= 0)
  204.       return;
  205.  
  206.    %pageLines = mFloor(%chatScrollHeight / %textHeight) - 1;
  207.    if (%pageLines <= 0)
  208.       %pageLines = 1;
  209.  
  210.    // See how many lines we actually can scroll up:
  211.    %chatPosition = -1 * getWord(%this.position, 1);
  212.    %linesToScroll = mFloor((%chatPosition / %textHeight) + 0.5);
  213.    if (%linesToScroll <= 0)
  214.       return;
  215.  
  216.    if (%linesToScroll > %pageLines)
  217.       %scrollLines = %pageLines;
  218.    else
  219.       %scrollLines = %linesToScroll;
  220.  
  221.    // Now set the position
  222.    %this.position = firstWord(%this.position) SPC (getWord(%this.position, 1) + (%scrollLines * %textHeight));
  223.  
  224.    // Display the pageup icon
  225.    chatPageDown.setVisible(true);
  226. }
  227.  
  228.  
  229. //-----------------------------------------------------------------------------
  230.  
  231. function ChatHud::pageDown(%this)
  232. {
  233.    // Find out the text line height
  234.    %textHeight = %this.profile.fontSize;
  235.    if (%textHeight <= 0)
  236.       %textHeight = 12;
  237.  
  238.    // Find out how many lines per page are visible
  239.    %chatScrollHeight = getWord(%this.getGroup().getGroup().extent, 1);
  240.    if (%chatScrollHeight <= 0)
  241.       return;
  242.  
  243.    %pageLines = mFloor(%chatScrollHeight / %textHeight) - 1;
  244.    if (%pageLines <= 0)
  245.       %pageLines = 1;
  246.  
  247.    // See how many lines we actually can scroll down:
  248.    %chatPosition = getWord(%this.extent, 1) - %chatScrollHeight + getWord(%this.position, 1);
  249.    %linesToScroll = mFloor((%chatPosition / %textHeight) + 0.5);
  250.    if (%linesToScroll <= 0)
  251.       return;
  252.  
  253.    if (%linesToScroll > %pageLines)
  254.       %scrollLines = %pageLines;
  255.    else
  256.       %scrollLines = %linesToScroll;
  257.  
  258.    // Now set the position
  259.    %this.position = firstWord(%this.position) SPC (getWord(%this.position, 1) - (%scrollLines * %textHeight));
  260.  
  261.    // See if we have should (still) display the pagedown icon
  262.    if (%scrollLines < %linesToScroll)
  263.       chatPageDown.setVisible(true);
  264.    else
  265.       chatPageDown.setVisible(false);
  266. }
  267.  
  268.  
  269. //-----------------------------------------------------------------------------
  270. // Support functions
  271. //-----------------------------------------------------------------------------
  272.  
  273. function pageUpMessageHud()
  274. {
  275.    ChatHud.pageUp();
  276. }
  277.  
  278. function pageDownMessageHud()
  279. {
  280.    ChatHud.pageDown();
  281. }
  282.  
  283. function cycleMessageHudSize()
  284. {
  285.    MainChatHud.nextChatHudLen();
  286. }
  287.